package org.x4444;

import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(mappedName = "jms/Queue")
public class SimpleMessageBean implements MessageListener {

	Logger logger = Logger.getLogger(getClass().getName());

	@Resource
	private MessageDrivenContext mdc;

	@Override
	public void onMessage(Message inMessage) {
		logger.info("onMessage");
		TextMessage msg = null;

		try {
			if (inMessage instanceof TextMessage) {
				msg = (TextMessage) inMessage;
				logger.info("MESSAGE BEAN: Message received: " + msg.getText());
			} else {
				logger.warning("Message of wrong type: " + inMessage.getClass().getName());
			}
		} catch (JMSException e) {
			e.printStackTrace();
			mdc.setRollbackOnly();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
